const express = require('express')
const Router = express.Router()
const expressJwt = require('express-jwt').expressjwt

// 拦截未登录的请求
Router.use(expressJwt({secret:'sujing', algorithms:['HS256']}).unless({
    path:['/user/login','/user/register']
}))


// 挂载【用户】路由表
Router.use('/user', require('./userRoutes'))

// 处理错误
Router.use((err, req, res, next)=>{
    let errMsg = {
        code:500,
        message:'错误'
    }
    if (err.name == 'UnauthorizedError') {
        errMsg.message = 'token验证失败'
        res.send(errMsg)
        return
    }
    errMsg.message = err.name
    res.send(errMsg)
    return
    next()
})

module.exports = Router










